
package xib.utils;
import java.util.Locale;

/**
 *
 * @author Alex
 */
public class EncodedString {

    private String string;
    
    private Locale loc;

    //TODO EncodedString() -> Redefined constructor comment
    /**
     * Constructor of an encoded string, language lowercase code as defined by
     * ISO-639 and country codes are three letter uppercase codes as defined by
     * ISO-3166.
     * @param string encoded string
     * @param language language which encodes the previous string
     */
     public EncodedString(String string, String language){
        this.string = string;

        String localLanguage = "";
        String country = "";

        // If there is a country specified along with the language we split the string
        if(language.indexOf("-") != -1){
        	String[] parts = language.split("-");
        	localLanguage = parts[0];
        	country = parts[1];
        }
        else
        	localLanguage = language;

        // Language and country validation
        try{
        	this.loc = null;

        	if(country.length() == 0)
        		loc = new Locale(localLanguage);
        	else
        		loc = new Locale(localLanguage,country);
        }
        catch(IllegalArgumentException ex){
            //TODO EncodedString() -> Defined exception if the language doesn't exist.
        }
    }

    /**
     * Function to obtain the language which encodes the string
     * @return String which specifies the language in a two-character ISO-639 format
     */
    public String getLanguage(){
        return loc.getLanguage();
    }

    /**
     * Function to obtain the country which specifies the language
     * of the encoded string
     * @return Country which specifies the language in upper-case two-character ISO-3166 format
     */
    public String getCountry(){
    	return loc.getCountry();
    }

    /**
     * Function to obtain the locale
     * @return
     */
    public Locale getLocale(){
        return loc;
    }

    /**
     * The stored string
     * @return stored string
     */
    public String getString(){
        return string;
    }
}
